﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace gamexpert.admin
{
    
    public partial class Products : System.Web.UI.Page
    {
        DataSet1 ds;
        DataSet1TableAdapters.my_productTableAdapter da;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!(User.Identity.IsAuthenticated && User.IsInRole("Administrator")))
            {
                Response.Redirect("~/admin/Default.aspx");
            }
            LinkButton lb = (LinkButton)Master.FindControl("lbtn_products");
            lb.CssClass = "selected";
            if (!IsPostBack)
            {
                MultiView1.ActiveViewIndex = 0;
            }
            ds = new DataSet1();
            da = new DataSet1TableAdapters.my_productTableAdapter();
            
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            databind_mygrid();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ds.my_product.Clear();
            cleanup();
            showdownload();
            MultiView1.ActiveViewIndex = 1;
        }
        protected void cleanup()
        {
            lblid.Text = "";
            txttitle.Text = "";
            txtshortdescription.Text = "";
            txtdescription.Text = "";
            txtprice.Text = "";
            txtshippingcost.Text = "";
            Image1.ImageUrl = "~/images/noimage.png";
            DropDownList2.SelectedIndex = 0;
            txtpublisher.Text = "";
            txtdeveloper.Text = "";
            txtplatform.Text = "";
            //chkpublished.Checked = true;
            txtkeywords.Text = "";
            lbldownload.Text = "";
        }
        
        protected void putdata(int id)
        {
            ds.my_product.Clear();
            da.FillByID(ds.my_product, id);
            DataSet1.my_productRow r = (DataSet1.my_productRow)ds.my_product.Rows[0];
                lblid.Text = r.id.ToString();
                txttitle.Text = r.title;
                txtshortdescription.Text = r.shortdescription;
                txtdescription.Text = r.description;
                txtprice.Text = r.price.ToString();
                txtshippingcost.Text = r.shippingcost.ToString();
                Image1.ImageUrl = "~/images/"+r.image;
                DropDownList2.SelectedValue = r.category;
                txtpublisher.Text = r.publisher;
                txtdeveloper.Text = r.developer;
                txtplatform.Text = r.platform;
                //chkpublished.Checked = r.published;
                txtkeywords.Text = r.keywords;
                lbldownload.Text = r.download;
                if (r.category == "Accessories")
                {
                    hidedownload();
                }
                else
                {
                    showdownload();
                }
        }

        protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            int id = Int32.Parse(GridView1.SelectedValue.ToString());
            putdata(id);
            MultiView1.ActiveViewIndex = 1;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            databind_mygrid();
            MultiView1.ActiveViewIndex = 0;
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList2.SelectedValue == "Accessories")
            {
                hidedownload();
            }
            else
            {
                showdownload();
            }
        }
        void hidedownload()
        {
            lbldownload.Text = "";
            FileUpload2.Visible = false;
            lbldownloadcaption.Text = "";
        }
        void showdownload()
        {
            FileUpload2.Visible = true;
            lbldownloadcaption.Text = "Download:";
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
  

                string prefix = Guid.NewGuid().ToString()+"_";
                string pic = "";
                string download = "";
                if (FileUpload1.HasFile)
                {
                    pic = prefix + FileUpload1.FileName;
                    string fileName = Path.Combine(Server.MapPath("~/images"), pic);
                    if (File.Exists(fileName))
                        File.Delete(fileName);
                    FileUpload1.SaveAs(fileName);
                }
                if (FileUpload2.HasFile)
                {
                    download = prefix + FileUpload2.FileName;
                    string fileName = Path.Combine(Server.MapPath("~/App_Data/upload"), download);
                    if (File.Exists(fileName))
                        File.Delete(fileName);
                    FileUpload2.SaveAs(fileName);
                }

                ds.my_product.Clear();
                int id = (lblid.Text.Trim()=="")?-1:Int32.Parse(lblid.Text);
                if (id > 0)
                {
                    da.FillByID(ds.my_product, id);
                    DataSet1.my_productRow r = (DataSet1.my_productRow)ds.my_product.Rows[0];
                    r.title = txttitle.Text;
                    r.shortdescription = txtshortdescription.Text;
                    r.description = txtdescription.Text;
                    r.price = Decimal.Parse(txtprice.Text);
                    r.shippingcost = Decimal.Parse(txtshippingcost.Text);
                    if (pic.Length > 0)
                    {
                        r.image = pic;
                    }
                    r.category = DropDownList2.SelectedValue;
                    r.publisher = txtpublisher.Text;
                    r.developer = txtdeveloper.Text;
                    r.platform = txtplatform.Text;
                    r.published = true;
                    r.keywords = txtkeywords.Text;
                    if (download.Length > 0)
                    {
                        r.download = download;
                    }
                }
                else
                {
                    ds.my_product.Addmy_productRow(txttitle.Text, txtshortdescription.Text, txtdescription.Text, Decimal.Parse(txtprice.Text), Decimal.Parse(txtshippingcost.Text), pic, DropDownList2.SelectedValue, DateTime.Now, txtpublisher.Text, txtdeveloper.Text, txtplatform.Text, true, txtkeywords.Text, download);
                }
                da.Update(ds.my_product);
                databind_mygrid();
                MultiView1.ActiveViewIndex = 0;
            
        }
        void databind_mygrid()
        {
            string category = DropDownList1.SelectedValue;
            if (category == "Games" || category == "Accessories")
            {
                //Clear any selet parameters
                SqlDataSource1.SelectParameters.Clear();

                //Create the SQL Command Text with an appened where clause
                String sqlSelectCommand = String.Concat(
                    SqlDataSource1.SelectCommand, " WHERE category LIKE '%'+@category+'%'");

                //set the SelectCommand to the sqlSelectCommand variable
                SqlDataSource1.SelectCommand = sqlSelectCommand;

                //Tell the SqlDataSource that we're uisng text as our SQL statement
                SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.Text;

                //Add the control parameter to the slectParameters collection
                SqlDataSource1.SelectParameters.Add(
                    new ControlParameter("category", "DropDownList1", "SelectedValue"));
            }
            else
            {
                SqlDataSource1.SelectCommand = "Select * from my_product";
            }
        }

        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            databind_mygrid();
        }
        

    }
}
